import { notNilEmpty, QXEvent } from 'qx-util';
import { TerminalMessageService } from '../terminal-message/terminal-message.service';

/**
 * console消息组
 *
 * @author chitanda
 * @date 2021-06-23 15:06:53
 * @export
 * @class GroupMessageBaseService
 */
export class GroupMessageBaseService {
  readonly evt: QXEvent<{ change: void }> = new QXEvent();
  /**
   * 默认分组
   *
   * @author chitanda
   * @date 2021-06-23 15:06:50
   */
  readonly defaultGroup = '默认';
  /**
   * 消息服务
   *
   * @author chitanda
   * @date 2021-06-23 15:06:04
   * @type {Map<string, TerminalMessageService>}
   */
  readonly map: Map<string, TerminalMessageService> = new Map();

  /**
   * 激活的分组
   *
   * @author chitanda
   * @date 2021-06-23 15:06:59
   * @type {string}
   */
  activeGroup: string = this.defaultGroup;

  /**
   * 锁定的分组
   *
   * @author chitanda
   * @date 2021-12-14 18:12:18
   * @type {string}
   */
  lockGroup: string = '';

  /**
   * Creates an instance of GroupMessageBaseService.
   * @author chitanda
   * @date 2021-06-23 15:06:41
   */
  constructor() {
    this.init();
  }

  /**
   * 初始化参数
   *
   * @author chitanda
   * @date 2021-06-23 15:06:12
   * @protected
   */
  protected init(): void {
    this.map.set(this.defaultGroup, new TerminalMessageService(this));
  }

  /**
   * 输出
   *
   * @author chitanda
   * @date 2021-06-23 15:06:55
   * @param {{ type: 'CONSOLE'; subtype?: string; content: string }} item
   */
  write(item: { type: 'CONSOLE'; subtype?: string; content: string }): void {
    if (notNilEmpty(item.subtype)) {
      if (!this.map.has(item.subtype)) {
        this.map.set(item.subtype, new TerminalMessageService(this));
      }
    }
    const type = item.subtype || this.defaultGroup;
    this.activeTab(type);
    const s: TerminalMessageService = this.map.get(type)!;
    s.writeln(item.content, type);
    this.emitChange();
  }

  /**
   * 销毁某个类型消息
   *
   * @author chitanda
   * @date 2021-06-23 15:06:12
   * @param {string} type
   * @return {*}  {boolean}
   */
  destroy(type: string): boolean {
    if (this.map.has(type) && type !== this.defaultGroup) {
      this.map.delete(type);
      this.emitChange();
      return true;
    }
    return false;
  }

  /**
   * 激活分页
   *
   * @author chitanda
   * @date 2021-12-14 19:12:23
   * @param {string} tab
   */
  activeTab(tab: string): boolean {
    if (this.lockGroup !== '' && this.lockGroup !== tab) {
      return false;
    }
    if (this.activeGroup !== tab) {
      this.activeGroup = tab;
      this.emitChange();
      return true;
    }
    return false;
  }

  /**
   * 锁定分页
   *
   * @author chitanda
   * @date 2021-12-14 18:12:46
   * @param {string} tab
   */
  lockTab(tab: string): boolean {
    if (this.lockGroup === tab) {
      this.lockGroup = '';
    } else {
      this.lockGroup = tab;
    }
    this.emitChange();
    return true;
  }

  /**
   * 发送变更事件
   *
   * @author chitanda
   * @date 2021-06-23 18:06:11
   */
  emitChange(): void {
    this.evt.emit('change');
  }
}
